//
//  DHAddCaseViewController.m
//  发;fjag
//
//  Created by 刘鹏飞 on 2016/11/14.
//  Copyright © 2016年 刘鹏飞. All rights reserved.
//

#import "DHAddCaseViewController.h"
#import <Masonry.h>
#import "XHHAdditions.h"
#import "DHBaseClassView.h"
#import "DHDescribeView.h"
#import "DHPicView.h"
#import "DHSubdivisionView.h"
#import "DHDCViewController.h"
#import <SVProgressHUD.h>
#import "DHDiseaseClassModel.h"
#import <HMImagePickerController.h>

#define delay(time) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((time) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{[SVProgressHUD dismiss];})
@interface DHAddCaseViewController ()<HMImagePickerControllerDelegate>

/*---疾病类型视图---*/
@property (nonatomic,weak)DHBaseClassView *classView;
/*---疾病细分视图---*/
@property (nonatomic,weak)DHSubdivisionView *subdivideView;
/*---疾病描述视图---*/
@property (nonatomic,weak)DHDescribeView *describeView;
/*---疾病描述视图---*/
@property (nonatomic,weak)DHPicView *picView;
/*---选择的cell---*/
@property (nonatomic,assign)NSUInteger index;
@property (nonatomic,copy)NSArray *describeModel;
// 选中照片数组
@property (nonatomic) NSArray *images;
// 选中资源素材数组，用于定位已经选择的照片
@property (nonatomic) NSArray *selectedAssets;
// 选中imgView
@property (nonatomic) NSArray *imgView;

@end

@implementation DHAddCaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}


#pragma mark - 创建子控件并布局子控件

-(void)xhhSetupNavigationItems {
    
    self.navigationItem.title = @"添加病例";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(sureAtion:)];
    
}

-(void)sureAtion:(UIBarButtonItem *)sender {
    if (self.classView.diseaseClass == nil) {
        //调用第三方弹出提示框
        [SVProgressHUD showErrorWithStatus:@"病例内容不容为空"];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        delay(1.0);
        return;
    }
    DHAddCaseModel *model = [[DHAddCaseModel alloc] init];
    model.caseCatogory = self.classView.diseaseClass;
    model.caseDetail = self.subdivideView.subdivision;
    model.describe = self.describeView.textView.text;
    model.desArray = self.describeView.tabArr;
    model.picArray = self.imgView;
    NSDateFormatter *formater = [[NSDateFormatter alloc]init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *time = [formater stringFromDate:[NSDate date]];
    model.time = time;
    self.myTapBlock(model);
    
    [self.navigationController popViewControllerAnimated:YES];
}

//创建并布局子控件
-(void)xhhSetupSubviews {
    
    //-------疾病分类视图--------//
    
    DHBaseClassView *classView = [DHBaseClassView baseClass];
    self.classView = classView;
    [self.view addSubview:classView];
    
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disableClassTapAction:)];
    [classView addGestureRecognizer:tapRecognizer];
    
    //-------疾病细分视图--------//
    DHSubdivisionView *subdivideView = [DHSubdivisionView subdivisionView];
    self.subdivideView = subdivideView;
    [self.view addSubview:subdivideView];
    
    UITapGestureRecognizer *tapRecognizerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(subdivisionTapAction:)];
    [subdivideView addGestureRecognizer:tapRecognizerOne];
    
    
    //-------疾病描述视图--------//
    DHDescribeView *describeView = [DHDescribeView describeView];
    self.describeView = describeView;
    [self.view addSubview:describeView];
    
    [describeView.addBtn addTarget:self action:@selector(describeTapAction:) forControlEvents:UIControlEventTouchUpInside];
    UITapGestureRecognizer *tapRecognizerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(describeTapAction:)];
    [describeView.addLabel addGestureRecognizer:tapRecognizerTwo];
    
    
    //-------疾病图片添加视图--------//
    DHPicView *picView = [DHPicView picView];
    self.picView = picView;
    [self.view addSubview:picView];
    UITapGestureRecognizer *tapRecognizerThree = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addImageAction:)];
    [picView.addImage addGestureRecognizer:tapRecognizerThree];
    void (^myBlock)() = ^(NSArray *arr){
        self.imgView = arr;
//        self.selectedAssets = arr;
        for (UIImageView *imageView in self.imgView) {
            imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tapRecognizerThree = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addImageAction:)];
            [imageView addGestureRecognizer:tapRecognizerThree];
        }
    };
    picView.myTapBlock = myBlock;
    
    //---------视图布局---------//
    //疾病分类视图布局
    [classView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.equalTo(self.view);
        make.top.equalTo(self.view).offset(64);
        make.height.mas_equalTo(40);
    }];
    //疾病细分视图布局
    [subdivideView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.equalTo(self.view);
        make.top.equalTo(classView.mas_bottom);
        make.height.mas_equalTo(40);
    }];
    //疾病描述视图布局
    [describeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.equalTo(self.view);
        make.top.equalTo(subdivideView.mas_bottom);
        make.height.mas_equalTo(220);
    }];
    //疾病图片视图布局
    [picView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.bottom.equalTo(self.view);
        make.top.equalTo(describeView.mas_bottom);
    }];
}


#pragma mark - 跳转交互

-(void)disableClassTapAction:(UITapGestureRecognizer *)sender {
    
    DHDCViewController *DHDC = [[DHDCViewController alloc] init];
    //准备好block
    void (^myBlock)() = ^(NSString *name,NSUInteger index){
        self.classView.diseaseClass = name;
        self.index = index;
    };
    NSMutableArray *arrM = [NSMutableArray array];
    [arrM addObjectsFromArray:@[@"肿瘤病",@"血液病",@"心脑血管病",@"神经系统",@"骨科"]];
    
    if (DHDC != nil) {
        DHDC.myBlock = myBlock;
        DHDC.tag = 0;
        DHDC.listArr = arrM;
    }
    
    [self.navigationController pushViewController:DHDC animated:YES];
    
}

-(void)subdivisionTapAction:(UITapGestureRecognizer *)sender {
    
    if (self.classView.diseaseClass == nil) {
        //调用第三方弹出提示框
        [SVProgressHUD showErrorWithStatus:@"请先选择疾病类型"];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        delay(1.0);
        return;
    }
    DHDCViewController *DHDC = [[DHDCViewController alloc] init];
    //准备好block
    void (^myBlock)() = ^(NSString *name){
        self.subdivideView.subdivision = name;
    };
    if (DHDC != nil) {
        DHDC.myBlock = myBlock;
        DHDC.tag = 1;
        NSLog(@"%zd",self.index);
        DHDC.ID = @(self.index + 1);
        int arr[] = {1,1,2,2,4};
        DHDC.num = (NSUInteger)arr[self.index];
        
    }
    
    [self.navigationController pushViewController:DHDC animated:YES];
    
}

-(void)describeTapAction:(UITapGestureRecognizer *)sender {
    
    DHDCViewController *DHDC = [[DHDCViewController alloc] init];
    
    NSMutableArray *arrM = [NSMutableArray array];
    [arrM addObjectsFromArray:@[@"腹痛",@"厌食",@"乏力",@"体重减轻",@"恶心",@"欧呕吐",@"呕血"]];
    NSMutableArray *arr = [NSMutableArray array];
    for (NSString *str in arrM) {
        DHDiseaseClassModel *model = [DHDiseaseClassModel new];
        model.isClick = NO;
        model.diseaseClass = str;
        [arr addObject:model];
    }
    //准备好block
    void (^myBlock)() = ^(NSArray *arr,NSArray *arrOne){
        self.describeView.tabArr = arr;
        self.describeModel = arrOne;
    };
    if (DHDC != nil && self.describeModel == nil) {
        DHDC.tag = 2;
        DHDC.myTapBlock = myBlock;
        DHDC.modelArr = arr;
    }else {
        DHDC.tag = 2;
        DHDC.myTapBlock = myBlock;
        DHDC.modelArr = self.describeModel;
    }
    [self.navigationController pushViewController:DHDC animated:YES];
    
}

-(void)addImageAction:(UITapGestureRecognizer *) sender{
    HMImagePickerController *picker = [[HMImagePickerController alloc] initWithSelectedAssets:self.selectedAssets];
    // 设置图像选择代理
    picker.pickerDelegate = self;
    // 设置目标图片尺寸
    picker.targetSize = CGSizeMake(600, 600);
    // 设置最大选择照片数量
    picker.maxPickerCount = 3;
    [self presentViewController:picker animated:YES completion:nil];
}


#pragma mark - 实现代理方法

- (void)imagePickerController:(HMImagePickerController *)picker didFinishSelectedImages:(NSArray<UIImage *> *)images selectedAssets:(NSArray<PHAsset *> *)selectedAssets {
    // 记录图像，方便在CollectionView 显示
    self.images = images;
    // 记录选中资源集合，方便再次选择照片定位
    self.selectedAssets = selectedAssets;
    self.picView.imageArr = images;
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
